var count = 0;
var track_searchService = {
	search : function(){
        var val = $.trim($('#textarea-search').val());

        if(val){
            val = val.replace(/，/g,',').replace(/[\r\n\s]/g, "");
        }else{
            this.setState({errors : true});
        }
        if(val.split(",").length > 10){
            this.setState({errors : true});
        }
        var param = val;

        this.setState({'search':param});

        if(param){
            this.setState({loading:true});
            Api.post("track_search.searchWayBillNos", {'urlParams' : param.split(',') }, this.handleSearchResult);
        }
	},
	handleChange : function(e){
        var val = e.target.value;
        this.setState({errors : false});
        if(val){
            val = val.replace(/，/g,',').replace(/[\r\n\s]/g, "");
        }else{
          this.setState({errors : true});
        }
        if(val.split(",").length > 10){
          this.setState({errors : true});
          val = '';
        }
		this.setState({originSearch : e.target.value, dirty : false});
	},
	handleSearchResult : function(res){
        var data = res.data,
            state = {loading:false, dirty : false};
        for(var i in data){
          data[i].open = true;
          for(var j in data[i]){
            data[i][j].unshift(true);
            break;
          }
          break;
        }
        if(i && j){
            state.list = data;
        }else{
          state.dirty = true;
          state.list = {};
        }
        this.setState(state);
	},
  openTrack : function(item){
    item.open = !item.open;
    this.fresh();
  },
  openChild : function(item){
    if(typeof item[0] === 'boolean'){
        item[0] = !item[0];
    }else{
        item.unshift(true);
    }
    this.fresh();
  },
  fresh : function(){
    this.setState({count : ++count});
  }
};
module.exports = track_searchService;